package thesis.ramstorage.math;

import java.util.Map;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RefineryUtilities;

import thesis.ramstorage.util.ChartRender;
import thesis.ramstorage.util.EvaluatedParameters;
import thesis.ramstorage.util.NodeParameters;

public class Evaluator {
	private DescriptiveStatistics stats = new DescriptiveStatistics();
	private long networkLoad;  
	private NodeParameters nodeMaxLoad = new NodeParameters(); 
	private NodeParameters nodeMinLoad = new NodeParameters();
	
	public Evaluator(EvaluatedParameters evaParams, boolean showChart) {
		// TODO Auto-generated constructor stub
		Map<Long, NodeParameters>  prms = evaParams.getNodeParams();
		for(Long k : prms.keySet()){
			long l = prms.get(k).getLoad();
			double averageLoadPerNode = l/prms.get(k).getCapacity();
			if(k == 0){
				this.nodeMinLoad.setLoad(l);
				this.nodeMinLoad.setID(0);
				
				this.nodeMaxLoad.setLoad(l);
				this.nodeMaxLoad.setID(0);
			}else{
				if(nodeMinLoad.getLoad() > averageLoadPerNode){
					this.nodeMinLoad.setLoad(l);
					this.nodeMinLoad.setID(k);
				}
				if(nodeMaxLoad.getLoad() < averageLoadPerNode){
					this.nodeMaxLoad.setLoad(l);
					this.nodeMaxLoad.setID(k);
				}
			}
			this.stats.addValue(averageLoadPerNode);
			networkLoad += l;
		}
		if(showChart){
			chartRender(prms);
		}
	}

	public Evaluator() {
		// TODO Auto-generated constructor stub
	}

	public void addValue( double value ){
		this.stats.addValue(value);
	}
	
	public double getMean(){
		return this.stats.getMean();
	}
	
	public double getStandardDeviation(){
		return this.stats.getStandardDeviation();
	}

	public long getNetworkLoad() {
		return networkLoad;
	}

	public void setNetworkLoad(long networkLoad) {
		this.networkLoad = networkLoad;
	}
	
	public double getMin(){
		return this.stats.getMin();
	}
	
	public double getMax(){
		return this.stats.getMax();
	}

	public NodeParameters getNodeMaxLoad() {
		return nodeMaxLoad;
	}

	public NodeParameters getNodeMinLoad() {
		return nodeMinLoad;
	}

	private void chartRender(Map<Long, NodeParameters> prms) {

//            ChartRender demo = new ChartRender("Network Statistic", prms);
//            demo.pack();
//            RefineryUtilities.centerFrameOnScreen(demo);
//            demo.setVisible(true);
    }
	

}
